<?php

declare(strict_types=1);

namespace app\index\controller;

use app\model\MessageCode;
use app\model\Post;
use app\model\User as ModelUser;
use app\model\UserCheckin;
use app\model\UserFriend;
use app\model\UserRecord;
use think\facade\View;
use think\Request;

class User extends Common
{

  public function initialize()
  {
    parent::initialize();

    if ($this->request->action() != 'index') {
      $this->forceLogin();
    }
  }

  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index()
  {
    //

    $id = $this->request->param('id');


    if (empty($id)) {
      $id = $this->modelUser->id;
    }

    if(empty($id)){
      return $this->error('访问错误', "Index/index");
    }

    $list_post = Post::where('user_id', $id)
      ->order('hits desc')
      ->where('is_verify', 1)
      ->limit(15)
      ->select();

    $list_record = UserRecord::whereIn('type', [
      'new_post'
    ])
      ->where('target_user_id', $id)
      ->order('id  desc')
      ->limit(10)
      ->select();

    $model_user = ModelUser::find($id);

    $model_user->readUser = $this->modelUser;

    View::assign('read_user', $model_user);
    View::assign('list_record', $list_record);
    View::assign('list_post', $list_post);
    return View::fetch();
  }
  public function set()
  {
    //

    return View::fetch();
  }

  public function message()
  {

    $list = UserRecord::whereIn('type', [
      'user_message'
    ])->order('id desc')
      ->paginate();

    View::assign('list', $list);

    return View::fetch();
  }

  public function optionFriend($user_id)
  {

    UserFriend::quickOptions($this->modelUser->id, $user_id);

    return json_message();
  }
  public function optionStatus($user_id)
  {

    $model_user = ModelUser::find($user_id);

    if ($model_user->status == 1) {
      $model_user->status = 0;
    } else {
      $model_user->status = 1;
    }

    $model_user->save();

    return json_message();
  }

  /**
   * 显示创建资源表单页.
   *
   * @return \think\Response
   */
  public function create()
  {
    //
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request)
  {
    //
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id)
  {
    //
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id)
  {
    //
  }

  /**
   * 保存更新的资源
   *
   * @param  \think\Request  $request
   * @param  int  $id
   * @return \think\Response
   */
  public function update(Request $request)
  {
    //

    $post_data = $request->post();

    // TODO:验证数据

    $this->modelUser->save($post_data);

    return json_message();
  }
  public function updatePassword(Request $request)
  {
    //

    $post_data = $request->post();

    // TODO:验证数据

    $post_data['password'] = md5($post_data['password'] . $this->modelUser->salt);


    $this->modelUser->save($post_data);

    return json_message();
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id)
  {
    //
  }

  public function emailVerify()
  {


    return View::fetch();
  }
  public function emailUpdate()
  {

    // TODO:验证数据,格式,新旧不能一样

    $post_data = $this->request->post();

    if ($this->modelUser->is_email_verify == 0) {
      $type = 'register_email';
    } else {
      $type = 'rebind_email';
    }

    $code = $post_data['code'];
    $email = $post_data['email'];

    if (!MessageCode::verify($email, $code, $type)) {

      return json_message('验证码错误');
    }

    $this->modelUser->email = $email;
    $this->modelUser->is_email_verify = 1;

    $this->modelUser->save();

    return json_message();
  }

  public function emailSendCode()
  {
    if ($this->modelUser->is_email_verify == 0) {
      $email = $this->modelUser->email;
      $type = 'register_email';
    } else {
      $email = $this->request->post('email');
      $type = 'rebind_email';

      // TODO:增加邮箱验证
    }
    try {
      MessageCode::buildAndSend($email, $type);
    } catch (\Throwable $th) {
      return json_message($th->getMessage());
    }

    return json_message();
  }

  public function authManage()
  {
    $list_user = ModelUser::where('auth_status', $this->request->param('auth_status', 3))
      ->order('id desc')
      ->paginate();


    View::assign('list_user', $list_user);

    return View::fetch();
  }

  public function updateAuthStatus($id, $auth_status, $message = '')
  {
    $model_user = ModelUser::find($id);

    $model_user->auth_status = $auth_status;

    $model_user->save();

    // TODO:给用户发送消息

    return json_message();
  }


  public function doCheckin()
  {
    UserCheckin::checkin();

    return json_message();
  }

  public function sendMessage($user_id)
  {
    $model_record = new UserRecord();

    $model_record->set('type', 'user_message');

    $model_record->target_id = $user_id;
    $model_record->target_user_id = $user_id;

    $model_record->from_user_id = $this->modelUser->id;
    $model_record->content = $this->request->post('content');
    $model_record->save();

    return json_message();
  }

  public function friends()
  {

    $target = $this->request->param('target', 'friend');

    if ($target == 'friend') {
      $with_target = 'user';
    } else {
      $with_target = 'friend';
    }

    $list_user = UserFriend::where($target . '_id', $this->modelUser->id)->paginate();

    $list_my_follower_count = UserFriend::where('friend_id', $this->modelUser->id)->count();
    $list_my_following_count = UserFriend::where('user_id', $this->modelUser->id)->count();
    View::assign('with_target', $with_target);
    View::assign('target', $target);
    View::assign('list_user', $list_user);
    View::assign('list_my_follower_count', $list_my_follower_count);
    View::assign('list_my_following_count', $list_my_following_count);
    return View::fetch();
  }
}
